﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RouteTiming.Data;

namespace RouteTiming.DynamicModel
{
    public class DynamicRoute
    {
        private Route _route;

        public Guid Id
        {
            get { return this._route.Id; }
        }

        public string Name
        {
            get { return this._route.Name; }
        }

        public DateTime ScheduledStartTime { get; private set; }

        public DateTime ScheduledEndTime { get; private set; }

        public List<DynamicRoutePoint> Points { get; private set; }

        public DynamicRoute(Route route, DateTime scheduledStartTime, DateTime scheduledEndTime)
        {
            this._route = route;
            this.ScheduledStartTime = scheduledStartTime;
            this.ScheduledEndTime = scheduledEndTime;

            // loading route points
            this.Points = new List<DynamicRoutePoint>();

            var pointQuery = from p in route.RoutePoints orderby p.OrderNo select p;
            foreach (var point in pointQuery)
            {
                var dynPoint = new DynamicRoutePoint(point, scheduledStartTime + point.PassTimeSpan);

                this.Points.Add(dynPoint);
            }
        }

        internal void ProcessEvent(Event evt)
        {
            var query = from p in this.Points 
                        where 
                            (p.Id == evt.RoutePointId) 
                            &&
                            (!p.IsPassed)
                        select p;

            foreach (var dynPoint in query)
            {
                dynPoint.PassTime = evt.TimeRaised;
            }
        }
    }
}
